package com.webchat.chat.web.auth;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.webchat.chat.core.baseEntity.Result;
import com.webchat.chat.core.baseEntity.ResultGenerator;
import com.webchat.chat.core.dto.loginDto.MenuReturnDTO;
import com.webchat.chat.core.tools.ChatConstant;
import com.webchat.chat.core.tools.ShiroUtil;
import com.webchat.chat.model.Permission;
import com.webchat.chat.service.PermissionService;
import com.webchat.chat.web.BaseController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by admin on 2018-06-29.
*/
@RestController
@RequestMapping(ChatConstant.CONTROLLER_ROOT_PATH + "/permission")
public class PermissionController extends BaseController {
    @Resource
    private PermissionService permissionService;

    @PostMapping("/all-menu")
    public Result getAllMenu() {
        List<MenuReturnDTO> menus = permissionService.findAllMenuByUserId(ShiroUtil.getUserId());
        return ResultGenerator.genSuccessResult(menus);
    }

    @PostMapping("/add")
    public Result add(Permission permission) {
        permissionService.save(permission);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam String id) {
        if (!"All".equals(ShiroUtil.getTenantCode())) {
            return ResultGenerator.genFailResult("没有对应操作权限，有需要请联系管理员！！");
        }
        permissionService.deletePermission(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(Permission permission) {
        if (!"All".equals(ShiroUtil.getTenantCode())) {
            return ResultGenerator.genFailResult("没有对应操作权限，有需要请联系管理员！！");
        }
        permissionService.update(permission);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam String id) {
        Permission permission = permissionService.findById(id);
        return ResultGenerator.genSuccessResult(permission);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Permission> list = permissionService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    @PostMapping("/findPermissionTree")
    public Result findPermissionTree() {
        List<Permission> list = permissionService.findPermissionTree();
        return ResultGenerator.genSuccessResult(list);
    }
}
